package com.hu.auth.common.utils;

import com.hu.auth.model.Menu;
import com.hu.auth.model.po.Meta;
import com.hu.auth.model.po.Router;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

// 菜单的工具类
public class MenuUtil {

    public static List<Menu> sort(List<Menu> allMenuList, Long menuId) {
        return allMenuList.stream()
                .filter(menu -> menu.getParentId().equals(menuId))
                .peek(menu -> menu.setChildren(sort(allMenuList, menu.getId())))
                .collect(Collectors.toList());
    }

    public static List<Router> sort1(List<Menu> menuList, Long menuId) {
        return menuList.stream()
                .filter(menu -> menu.getParentId().equals(menuId))
                .map(menu -> {
                    Router router = new Router();
                    router.setHidden(false);
                    router.setAlwaysShow(menu.getType().intValue() != 1);
                    router.setPath(getRouterPath(menu));
                    router.setComponent(menu.getComponent());
                    router.setMeta(new Meta(menu.getName(), menu.getIcon()));
                    router.setChildren(sort1(menuList, menu.getId()));
                    return router;
                })
                .collect(Collectors.toList());
    }

    private static String getRouterPath(Menu menu) {
        String routerPath = "/" + menu.getPath();
        if (menu.getParentId().intValue() != 0) {
            routerPath = menu.getPath();
        }
        return routerPath;
    }

    public static List<Router> buildRouters(List<Menu> menus) {
        List<Router> routers = new LinkedList<>();
        for (Menu menu : menus) {
            Router router = new Router();
            router.setHidden(false);
            router.setAlwaysShow(false);
            router.setPath(getRouterPath(menu));
            router.setComponent(menu.getComponent());
            router.setMeta(new Meta(menu.getName(), menu.getIcon()));
            List<Menu> children = menu.getChildren();
            //如果当前是菜单，需将按钮对应的路由加载出来，如：“角色授权”按钮对应的路由在“系统管理”下面
            if (menu.getType().intValue() == 1) {
                List<Menu> hiddenMenuList = children.stream().filter(item -> !StringUtils.isEmpty(item.getComponent())).collect(Collectors.toList());
                for (Menu hiddenMenu : hiddenMenuList) {
                    Router hiddenRouter = new Router();
                    hiddenRouter.setHidden(true);
                    hiddenRouter.setAlwaysShow(false);
                    hiddenRouter.setPath(getRouterPath(hiddenMenu));
                    hiddenRouter.setComponent(hiddenMenu.getComponent());
                    hiddenRouter.setMeta(new Meta(hiddenMenu.getName(), hiddenMenu.getIcon()));
                    routers.add(hiddenRouter);
                }
            } else {
                if (!CollectionUtils.isEmpty(children)) {
                    if (children.size() > 0) {
                        router.setAlwaysShow(true);
                    }
                    router.setChildren(buildRouters(children));
                }
            }
            routers.add(router);
        }
        return routers;
    }

    private static List<Router> sortChild(List<Menu> menuList, Long menuId) {
        return menuList.stream()
                .filter(menu -> !menu.getParentId().equals(menuId))
                .map(menu -> {
                    Router router = new Router();
                    router.setHidden(false);
                    router.setAlwaysShow(false);
                    router.setPath(getRouterPath(menu));
                    router.setComponent(menu.getComponent());
                    router.setMeta(new Meta(menu.getName(), menu.getIcon()));
                    return router;
                })
                .collect(Collectors.toList());
    }

}
